/**
 * @program MHAG
 * @ Charm Dialog Class
 * @version 1.0
 * @author Tifa@mh3
 *
 */
package org.mhag.swing;

/**
 *
 * @author lvmy
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JComboBox;
import org.mhag.model.*;

public class CharmDialog extends javax.swing.JDialog {

    /** Creates new form CharmDialog */
    public CharmDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public CharmDialog(java.awt.Frame parent, boolean modal, Mhag aMhag, MhagData aMhagData, Generator aGen) {
        initComponents();
		setTitle(String.format("My Charm (max %d)", numMax));
		mhagData = aMhagData;
		mhag = aMhag;
		gen = aGen;
		charmReload();
		LAFGUI.setupAutoComplete(charmSkill1);
		LAFGUI.setupAutoComplete(charmSkill2);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        charmSkillLabel = new javax.swing.JLabel();
        charmSkill1 = new javax.swing.JComboBox();
        skillPoint1 = new javax.swing.JComboBox();
        charmSkill2 = new javax.swing.JComboBox();
        skillPoint2 = new javax.swing.JComboBox();
        charmSlot = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        charmList = new javax.swing.JList();
        buttonAdd = new javax.swing.JButton();
        buttonRemove = new javax.swing.JButton();
        buttonUp = new javax.swing.JButton();
        buttonDown = new javax.swing.JButton();
        buttonSave = new javax.swing.JButton();
        buttonReload = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("");
        setResizable(false);
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
                formWindowLostFocus(evt);
            }
        });

        charmSkillLabel.setFont(new java.awt.Font("Monospaced", 0, 12));
        charmSkillLabel.setText("Slot");

        charmSkill1.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        charmSkill1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                charmSkill1ActionPerformed(evt);
            }
        });

        skillPoint1.setFont(new java.awt.Font("Monospaced", 0, 12));
        skillPoint1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0" }));
        skillPoint1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                skillPoint1ActionPerformed(evt);
            }
        });

        charmSkill2.setFont(new java.awt.Font("Monospaced", 0, 12)); // NOI18N
        charmSkill2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                charmSkill2ActionPerformed(evt);
            }
        });

        skillPoint2.setFont(new java.awt.Font("Monospaced", 0, 12));
        skillPoint2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0" }));
        skillPoint2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                skillPoint2ActionPerformed(evt);
            }
        });

        charmSlot.setFont(new java.awt.Font("Monospaced", 0, 12));
        charmSlot.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No Slot", "One Slot", "Two Slot", "Three Slot" }));
        charmSlot.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                charmSlotActionPerformed(evt);
            }
        });

        charmList.setBorder(javax.swing.BorderFactory.createTitledBorder("Charm List"));
        charmList.setFont(new java.awt.Font("Monospaced", 0, 12));
        charmList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "---" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        charmList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                charmListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(charmList);

        buttonAdd.setFont(new java.awt.Font("Monospaced", 0, 12));
        buttonAdd.setText("Add");
        buttonAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonAddActionPerformed(evt);
            }
        });

        buttonRemove.setFont(new java.awt.Font("Monospaced", 0, 12));
        buttonRemove.setText("Remove");
        buttonRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRemoveActionPerformed(evt);
            }
        });

        buttonUp.setFont(new java.awt.Font("Monospaced", 0, 12));
        buttonUp.setText("Move Up");
        buttonUp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonUpActionPerformed(evt);
            }
        });

        buttonDown.setFont(new java.awt.Font("Monospaced", 0, 12));
        buttonDown.setText("Move Down");
        buttonDown.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonDownActionPerformed(evt);
            }
        });

        buttonSave.setFont(new java.awt.Font("Monospaced", 0, 12));
        buttonSave.setText("Save");
        buttonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveActionPerformed(evt);
            }
        });

        buttonReload.setFont(new java.awt.Font("Monospaced", 0, 12));
        buttonReload.setText("Reload");
        buttonReload.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonReloadActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 327, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(mainPanelLayout.createSequentialGroup()
                            .addComponent(buttonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(buttonRemove, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, mainPanelLayout.createSequentialGroup()
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(charmSkill2, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(charmSkill1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(skillPoint1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(skillPoint2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(mainPanelLayout.createSequentialGroup()
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(buttonReload, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(buttonUp, javax.swing.GroupLayout.DEFAULT_SIZE, 100, Short.MAX_VALUE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(buttonSave, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(buttonDown, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(charmSkillLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(charmSlot, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, 0, 0, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, mainPanelLayout.createSequentialGroup()
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(charmSlot, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(charmSkillLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(charmSkill1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(skillPoint1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(charmSkill2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(skillPoint2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(buttonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(buttonRemove, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(buttonUp, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(buttonDown, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(buttonSave, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(buttonReload, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 573, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	private void charmSkill1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_charmSkill1ActionPerformed
		charmSkillAction(0);
}//GEN-LAST:event_charmSkill1ActionPerformed

	private void skillPoint1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_skillPoint1ActionPerformed
		charmPointAction(0);
}//GEN-LAST:event_skillPoint1ActionPerformed

	private void charmSkill2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_charmSkill2ActionPerformed
		charmSkillAction(1);
}//GEN-LAST:event_charmSkill2ActionPerformed

	private void skillPoint2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_skillPoint2ActionPerformed
		charmPointAction(1);
}//GEN-LAST:event_skillPoint2ActionPerformed

	private void charmSlotActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_charmSlotActionPerformed
		charmMenuAction();
}//GEN-LAST:event_charmSlotActionPerformed

	private void buttonAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonAddActionPerformed
		charmAddAction();
	}//GEN-LAST:event_buttonAddActionPerformed

	private void buttonRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRemoveActionPerformed
		charmRemoveAction();
	}//GEN-LAST:event_buttonRemoveActionPerformed

	private void buttonUpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonUpActionPerformed
		charmUpAction();
	}//GEN-LAST:event_buttonUpActionPerformed

	private void buttonDownActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDownActionPerformed
		charmDownAction();
	}//GEN-LAST:event_buttonDownActionPerformed

	private void buttonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveActionPerformed
		charmSave();
	}//GEN-LAST:event_buttonSaveActionPerformed

	private void buttonReloadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonReloadActionPerformed
		charmReload();
	}//GEN-LAST:event_buttonReloadActionPerformed

	private void charmListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_charmListValueChanged
		inputCharm();
	}//GEN-LAST:event_charmListValueChanged

	private void formWindowLostFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowLostFocus
		setVisible(false);
	}//GEN-LAST:event_formWindowLostFocus

    private void charmMenuAction()
    {
	   int nSlot = charmSlot.getSelectedIndex();
	   if(nSlot < 0)return;
	   charm.setNumSlot(nSlot);

	   changeCharmSkill(0, 2, nSlot, true);
	   changeCharmPoint(0, 2, nSlot, -1, true);
	   changeCharmSkill(1, 2, nSlot, false);
	   changeCharmPoint(1, 2, nSlot, -1, false);
    }

    private void charmSkillAction(int slotInd)
    {
	    int nSlot = charmSlot.getSelectedIndex();
	    if(slotInd == 0)
	    {
		   //charm skill 1
		   int ind = charmSkill1.getSelectedIndex();
		   if(ind <= 0)
		   {
			   changeCharmPoint(0, 2, nSlot, -1, true);
			   changeCharmSkill(1, 2, nSlot, false);
			   changeCharmPoint(1, 2, nSlot, -1, false);
			   return;
		   }

		   int[] list = mhagData.getSkillList(2, nSlot);   // deault g rank
		   charm.setSkillID(0,list[ind-1]);
		   charm.setNumSkill(0);   //no charm skill determined

		   changeCharmPoint(0, 2, nSlot, charm.getSkillID(0), true);
		   changeCharmSkill(1, 2, nSlot, false);
		   changeCharmPoint(1, 2, nSlot, -1, false);
	    }
	    else
	    {
		    //charm skill 2
		   int ind = charmSkill2.getSelectedIndex();
		   if(ind <= 0)
		   {
			   changeCharmPoint(1, 2, nSlot, -1, true);
			   return;
		   }

		   int[] list = mhagData.getSkillList(2, nSlot, charm.getSkillID(0), "Auto-Guard");
		   charm.setSkillID(1,list[ind-1]);
		   charm.setNumSkill(1);  // only the 1st charm skill determined

		   changeCharmPoint(1, 2, nSlot, charm.getSkillID(1), true);

	    }
    }

    private void charmPointAction(int slotInd)
    {
	    int rank = 2;
	    int nSlot = charm.getNumSlot();
	    if(slotInd == 0)
	    {
		   //charm skill point 1
		   int ind = skillPoint1.getSelectedIndex();
		   int skillID = charm.getSkillID(0);
		   if((ind < 0) || (skillID < 0))
		   {
			   charm.setNumSkill(0);
			   changeCharmPoint(0, rank, nSlot, -1, true);
			   changeCharmSkill(1, rank, nSlot, false);
			   changeCharmPoint(1, rank, nSlot, -1, false);
			   return;
		   }

		   Skill skill = mhagData.getSkill(skillID);

		   int maxPoint = skill.getMaxSkillPoint(rank, nSlot);
		   charm.setSkillPoint(0,maxPoint - ind);

		   if(skill.getSkillName().equals("Auto-Guard"))
			   charm.setSkillPoint(0,10);

		   if(charm.getSkillPoint(0) != 0)
		   {
			   charm.setNumSkill(1);
			   changeCharmSkill(1, rank, nSlot, true);
			   changeCharmPoint(1, rank, nSlot, -1, true);
		   }
		   else
		   {
			   charm.setNumSkill(0);
			   changeCharmSkill(1, rank, nSlot, false);
			   changeCharmPoint(1, rank, nSlot, -1, false);
		   }

	    }
	    else
	    {
		   //charm skill point 2
		   int ind = skillPoint2.getSelectedIndex();
		   int skillID = charm.getSkillID(1);

		   if((ind < 0) || (skillID < 0))
		   {
			   charm.setNumSkill(0);
			   changeCharmPoint(1, rank, nSlot, -1, true);
			   return;
		   }

		   Skill skill = mhagData.getSkill(skillID);
		   int maxPoint = skill.getMaxSkillPoint(rank, nSlot);

		   charm.setSkillPoint(1,maxPoint - ind);
		   if(charm.getSkillPoint(1) != 0)
			   charm.setNumSkill(2);
		   else
			   charm.setNumSkill(1);

	    }
    }

    public void changeCharmSkill(int skillNo, int rank, int nSlot, boolean active)
    {

	    JComboBox skillSlot;
	    int exception;
	    int[] ind;
	    if(skillNo == 0)
	    {
		    skillSlot = charmSkill1;
	   	    ind = mhagData.getSkillList(rank, nSlot);
	    }
	    else
	    {
		    skillSlot = charmSkill2;
		    exception = charm.getSkillID(0);
	   	    ind = mhagData.getSkillList(rank, nSlot, exception, "Auto-Guard");
	    }

	    if(!active)
	    {
		    //skillSlot.setVisible(false);
		    skillSlot.setEnabled(false);
		    return;
	    }

		int num = ind.length;

		skillSlot.removeAllItems();
		skillSlot.addItem("");
		for(int i = 0; i < num; i++)
		{
			Skill skill = mhagData.getSkill(ind[i]);
			skillSlot.addItem(skill.getSkillName());
		}
		skillSlot.setSelectedIndex(0);

	    //skillSlot.setVisible(true);
		skillSlot.setEnabled(true);
	    charm.setSkillID(skillNo,-1);

    }

    public void changeCharmPoint(int skillNo, int rank, int nSlot, int skillID, boolean active)
    {

	    JComboBox skillPoint;
	    if(skillNo == 0)
	    {
		    skillPoint = skillPoint1;
	    }
	    else
	    {
		    skillPoint = skillPoint2;
	    }

	    if(!active)
	    {
		    //skillPoint.setVisible(false);
			skillPoint.setEnabled(false);
		    return;
	    }

	    if(skillID < 0)
	    {
			skillPoint.removeAllItems();
			charm.setSkillPoint(skillNo,0);
		    //skillPoint.setVisible(false);
			skillPoint.setEnabled(false);
	    }
	    else
	    {
		    Skill skill = mhagData.getSkill(skillID);
		    int maxPoint = skill.getMaxSkillPoint(rank, nSlot);
		    int minPoint = 0;
		    if(skill.getSkillName().equals("Auto-Guard"))
			    minPoint = 10;   // fixed skill point for auto-guard
		    if(skillNo == 1)
			    minPoint = -10;

			skillPoint.removeAllItems();
			int j = 0;
			for(int i = maxPoint; i >= minPoint; i--)
			{
				skillPoint.addItem(i);
				if(i == 0)
					skillPoint.setSelectedIndex(j);
				j++;

			}
		    if(skill.getSkillName().equals("Auto-Guard"))
		    {
			    skillPoint.setSelectedIndex(0);
			    //skillPoint.setVisible(true);
			    skillPoint.setEnabled(true);
			    charm.setSkillPoint(skillNo,10);
			    charm.setNumSkill(1);
			    return;
		    }

		    //skillPoint.setVisible(true);
			skillPoint.setEnabled(true);
		    charm.setSkillPoint(skillNo,0);
	    }
	    if(skillNo == 0)
		    charm.setNumSkill(0);
	    else
		    charm.setNumSkill(1);

    }

	// charm add button action
	private void charmAddAction()
	{
		if(numTot >= numMax)return;
		charm.setCharmName(mhagData);
		String name = charm.getCharmName();
		//System.out.println(name);
		if(name.length() <= 0)return; // no charm specified

		// add charm to the list
		listModel.insertElementAt(name, numTot);
		charmList.setSelectedIndex(numTot);
		numTot++;
	}

	// charm list remove
	public void charmRemoveAction()
	{
		if(numTot <= 0)return; // no charm

		int ind = charmList.getSelectedIndex();
		if(ind < 0)return;  // no entry

		//remove charm from the list
		listModel.removeElementAt(ind);
		if(ind > 0 )
			charmList.setSelectedIndex(ind - 1);
		else
			charmList.setSelectedIndex(0);
		numTot--;
	}

	// charm list move up
	public void charmUpAction()
	{
		int ind = charmList.getSelectedIndex();
		if(ind <= 0)return;   //first charm can't be moved up

		String name = listModel.getElementAt(ind).toString();

		listModel.setElementAt(listModel.getElementAt(ind - 1), ind);
		listModel.setElementAt(name, ind - 1);
		charmList.setSelectedIndex(ind - 1);
	}

	// charm list move down
	public void charmDownAction()
	{
		int ind = charmList.getSelectedIndex();
		if(ind >= numTot - 1)return;   //last charm can't be moved down

		String name = listModel.getElementAt(ind).toString();

		listModel.setElementAt(listModel.getElementAt(ind + 1), ind);
		listModel.setElementAt(name, ind + 1);
		charmList.setSelectedIndex(ind + 1);
	}

	//charm save
	public void charmSave()
	{
		try {
			PrintStream fileout;
			fileout = new PrintStream(new File(mhagData.getDirSave(mhag.getGame()) + fileCharm));
			for(int i = 0; i < numTot; i++)
			{
				fileout.println(listModel.getElementAt(i).toString());
			}
			fileout.close();
		} catch (FileNotFoundException ex) {
			Logger.getLogger(CharmDialog.class.getName()).log(Level.SEVERE, null, ex);
		}

	}

	//charm reload
	public final void charmReload()
	{
		adjust = true;
	    listModel.clear();
		charmSlot.setSelectedIndex(0);

		numTot = 0;
		try {
			Scanner filein = new Scanner(new File(mhagData.getDirSave(mhag.getGame()) + fileCharm));

			while(filein.hasNext())
			{
				String line = filein.nextLine();
				listModel.insertElementAt(line, numTot);
				numTot++;
			}

		} catch (FileNotFoundException ex) {
			listModel.insertElementAt("Auto-Guard +10", 0);
			listModel.insertElementAt("OOO", 1);
			listModel.insertElementAt("OO", 2);
			numTot = 3;
		}
	    charmList.setModel(listModel);
		adjust = false;

	    charmList.setSelectedIndex(numTot - 1);
	}


	// input charm from charm list
	public void	inputCharm()
	{
		int index = charmList.getSelectedIndex();
		if(index < 0)return;
		String line = listModel.getElementAt(index).toString();
		Charm aCharm = new Charm();
		aCharm.setCharmFromLine(line, mhagData);

		charmSlot.setSelectedIndex(aCharm.getNumSlot());
		if(aCharm.getNumSkill() >= 1)
		{
			Skill skill = mhagData.getSkill(aCharm.getSkillID(0));
			charmSkill1.setSelectedItem(skill.getSkillName());
			skillPoint1.setSelectedItem(aCharm.getSkillPoint(0));
		}
		if(aCharm.getNumSkill() >= 2)
		{
			Skill skill = mhagData.getSkill(aCharm.getSkillID(1));
			charmSkill2.setSelectedItem(skill.getSkillName());
			skillPoint2.setSelectedItem(aCharm.getSkillPoint(1));
		}
	}

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
			@Override
            public void run() {
                CharmDialog dialog = new CharmDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					@Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

	// my variables
	private MhagData mhagData;
	private Mhag mhag;
	private Generator gen;
	private Charm charm = new Charm();
	private int numTot = 0;
	private DefaultListModel listModel =  new DefaultListModel(); //generator skill list
	private boolean adjust = false;

	private int numMax = MhagData.getNMaxCharm();  //maximum number of charms
	private String fileCharm = MhagData.getFileCharm();

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonAdd;
    private javax.swing.JButton buttonDown;
    private javax.swing.JButton buttonReload;
    private javax.swing.JButton buttonRemove;
    private javax.swing.JButton buttonSave;
    private javax.swing.JButton buttonUp;
    private javax.swing.JList charmList;
    private javax.swing.JComboBox charmSkill1;
    private javax.swing.JComboBox charmSkill2;
    private javax.swing.JLabel charmSkillLabel;
    private javax.swing.JComboBox charmSlot;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JComboBox skillPoint1;
    private javax.swing.JComboBox skillPoint2;
    // End of variables declaration//GEN-END:variables

}
